﻿using System;
using System.Web.UI;
using Microsoft.SharePoint.Utilities;
using System.Web.UI.WebControls;
using Evolution.Expression;

namespace Evolution.UI.WebParts
{
    [Serializable]
    public class TransferToErrorPageAction : BaseAction
    {
        #region Constructors

        public TransferToErrorPageAction()
        {

        }

        public TransferToErrorPageAction(string message)
        {
            this.Message = message;
        }

        #endregion

        #region Properties

        public string Message { get; set; }

        public override string Caption
        {
            get { return "Transfer to an error page"; }
        }

        public override bool RequireCondition
        {
            get { return true; }
            set { }
        }

        #endregion

        #region Methods

        public override void CreateInterface(ControlCollectionBuilder builder, PageActionEditor editor)
        {
            builder.AddStaticText(@"<div class=""UserControlBody"" style=""margin-top: 5px"">Type the message.</div>");
            builder.AddStaticText(@"<div class=""UserControlGroup"" style=""padding-bottom: 2px"">");

            TextBox message = new TextBox();
            message.ID = "Evolution_Pawp_Message";
            message.CssClass = "UserInput";
            message.Style[HtmlTextWriterStyle.Width] = "175px";

            builder.AddTextBoxAndBuilder(message, false);

            builder.AddStaticText("</div>");

            builder.AddErrorPlaceholder("Message");
        }

        public override void Execute(Page page, PageActionWebPart part)
        {
            ExpressionInterpreter interp = PageActionWebPart.GetInterpreter(page, part);
            string message = interp.ParseExpression(this.Message);
            SPUtility.TransferToErrorPage(message);
        }

        public override string GetDescription()
        {
            return "Transfer to error page";
        }

        public override void ApplyChanges(PageActionEditor editor)
        {
            TextBox text3 = editor.FindControl("Evolution_Pawp_Message") as TextBox;
            if (text3.Text.Trim() == String.Empty)
                editor.RaiseError("Message", "<br/>Enter a message to display in the error page.", String.Format("The action '{0}' requires the indication of a error message.", this.Caption));

            this.Message = text3.Text;
        }

        public override void SyncChanges(PageActionEditor editor)
        {            
            (editor.FindControl("Evolution_Pawp_Message") as TextBox).Text = this.Message;
        }

        #endregion
    }
}
